package cn.bocweb.spinning.view.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.view.adapter.MyMsgAdapter;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.fragment.ReplyMsgFragment;
import cn.bocweb.spinning.view.fragment.SystemMsgFragment;

public class MyMsgActivity extends BaseActivity {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.viewPager)
    ViewPager viewPager;
    @Bind(R.id.tabLayout)
    TabLayout tabLayout;

    MyMsgAdapter adapter;
    TextView myMsgSystemUnread;
    List<Fragment> list;
    ReplyMsgFragment replyMsgFragment;
    SystemMsgFragment systemMsgFragment;
    TextView replyUnread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_msg);
        ButterKnife.bind(this);
        initToolbar();
        initViewPager();
    }

    private void initToolbar() {
        toolbar.setTitle("消息");
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        toolbar.setNavigationIcon(R.mipmap.btn_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initViewPager() {

        replyMsgFragment = new ReplyMsgFragment();
        systemMsgFragment = new SystemMsgFragment();

        list = new ArrayList<>();
        list.add(replyMsgFragment);
        list.add(systemMsgFragment);

        adapter = new MyMsgAdapter(getSupportFragmentManager(), list);
        viewPager.setAdapter(adapter);

        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.colorPrimary));

        View view1 = LayoutInflater.from(this).inflate(R.layout.view_msg, null);
        replyUnread = (TextView) view1.findViewById(R.id.reply_unread);
        TabLayout.Tab tab1 = tabLayout.getTabAt(0);
        tab1.setCustomView(view1);

        View view2 = LayoutInflater.from(this).inflate(R.layout.view_msg_system, null);
        myMsgSystemUnread = (TextView) view2.findViewById(R.id.my_msg_system_unread);
        TabLayout.Tab tab2 = tabLayout.getTabAt(1);
        tab2.setCustomView(view2);
    }

    public void setSystemUnread(String unread){
        if (unread.equals("0")) {
            myMsgSystemUnread.setVisibility(View.GONE);
        } else {
            myMsgSystemUnread.setText(String.valueOf(unread));
            myMsgSystemUnread.setVisibility(View.VISIBLE);
        }
    }

    public void setReplyMsgUnread(String unread) {
        if (unread.equals("0")) {
            replyUnread.setVisibility(View.GONE);
        } else {
            replyUnread.setText(unread);
            replyUnread.setVisibility(View.VISIBLE);
        }
    }
}
